package slerp;

/**
 * A simple class to act as a clock, using the nanoTime() from the System package.
 * @author Pasta
 */
public class HerpEngineTime {
    long start, time, lastTime, pauseRes = 0, paused = 0;
    boolean notStarted, isPaused = true;
    static HerpEngineTime timer = new HerpEngineTime();
    /**
     * Constructor for the class.
     */
    public HerpEngineTime() {  
    }
    
    /**
     * Starts the clock.
     */
    public void start() {
        isPaused = false;
        long pause_delta = pauseRes - paused;
        start = System.nanoTime() + pause_delta;
    }
    
    /**
     * Pauses the timer.
     */
    public void pause() {
        isPaused = true;
        paused = System.nanoTime();
    }
    
    /**
     * Resets the timer.
     */
    public void reset() {
        time = 0;
        pauseRes = 0;
        paused = 0;
        start = 0;
        isPaused = true;
        lastTime = 0;
    }
    
    /**
     * Method to get the time since the clock was started.
     * @return The time since the clock was started.
     */
    public long getTime() {
        if(notStarted)
            return 0;
        else if(!isPaused)
            return System.nanoTime() - start;
        else
            return paused - start;
    }
    
    /**
     * Method to get the time since the last call of the method.
     * If it was the first time the method was called, it will return the time 
     * since it started.
     * @return The time since the last call of getDeltaTime.
     */
    public long getDeltaTime() {
        long timed = System.nanoTime();
        if(isPaused) {
            return 0;
        } else if(lastTime == 0) {
            lastTime = start;
        }  
        long derp = System.nanoTime() - lastTime;
        lastTime = timed;
        return derp;
    }
}
